import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import {Book} from './entities/book.entity';
import { CreateBookDto } from './dto/create-book.dto';
import { UpdateBookDto } from './dto/update-book.dto';
import OpenAI from "openai"

const openai = new OpenAI({
  apiKey: 'sk-proj-RRONkAXm39EJcaXgQEFoT3BlbkFJGqjO5Nwc4gOjtLQMAKrp',
  baseURL:'http://openai.bayesbp.cn/v1/'
});

@Injectable()
export class BookService {
  constructor(
    @InjectRepository(Book)
    private usersRepository: Repository<Book>,
  ) {}
  async create(createBookDto: CreateBookDto) {
    const book = new Book();
    book.name = createBookDto.name;
    book.img = createBookDto.img;
    book.mobile = createBookDto.mobile;
    book.content = createBookDto.content;
    await book.save();
  }
  async list(listBookDto: any) {
    let data = await Book.find({order: {  
      created_at: 'DESC'  
    }});
    return data
  }
  async findAll():Promise<Book[]> {
    const allBooks = await Book.find();
    return allBooks;
  }

  async findOne(id: number):Promise<Book | null> {
    return null
  }

  update(id: number, updateBookDto: UpdateBookDto) {
    return `This action updates a #${id} book`;
  }

  remove(id: number) {
    return `This action removes a #${id} book`;
  }

  // 调用openai api
  async chat(req:any){
    console.log(req)
    const response = await openai.chat.completions.create({
      model: "gpt-4o",
      messages: [
        {
          "role": "user",
          "content": [
            {
              "type": "text",
              "text": req.template
            },
            {
              "type": "image_url",
              "image_url": {
                "url": req.img
              }
            }
          ]
        }
      ]
    });
    return response?.choices?.[0]?.message?.content;
  }
  
  //获取提问模板
  getTemplate(){
    let list = [
      {
        id:1,

      }
    ]
  }
}
